{include file="public/header" /}
<div class="page">
  <div class="fixed-empty"></div>
  <form id="adv_form" enctype="multipart/form-data" method="post" name="advForm" action="{if isset($adv) && $adv.adv_images !=''}{:url('Adv/adv_edit')}{else/}{:url('Adv/adv_add')}{/if}" >
    <table class="ds-default-table">
      <tbody>
        <tr class="noborder">
          <td class="required w120"><label class="validation" for="adv_name">{$Think.lang.adv_name}:</label></td>
          <td class="vatop rowform"><input type="text" name="adv_name" id="adv_name" class="txt" value="{$adv.adv_title|default=''}"></td>
          <td class="vatop tips"></td>
        </tr>
        <tr class="noborder">
            <td class="required w120"><label class="validation" >所在位置:</label></td>
            <td class="vatop rowform">
                <select name="adv_address" id="adv_address">
                    <option value='1' {if condition="$adv.adv_address eq 1"}selected{/if}>客户端主页</option>
                    <option value='2' {if condition="$adv.adv_address eq 2"}selected{/if}>客户端商城</option>
                    <option value='3' {if condition="$adv.adv_address eq 3"}selected{/if}>服务端培训</option>
                </select>
            </td>
            <td class="vatop tips"></td>
        </tr>

        <tr class="noborder">
            <td class="required"><label for="adv_startdate">类型:</label></td>
            <td class="vatop rowform">
                <div class="onoff">
                    <label for="adv_type1" class="cb-enable {if condition="$adv.adv_type eq 1"}selected{/if}">轮播图</label>
                    <label for="adv_type0" class="cb-disable {if condition="$adv.adv_type eq 2"}selected{/if}">广告图</label>
                    <input id="adv_type1" name="adv_type" value="1" type="radio" {if condition="$adv.adv_type eq 1"} checked="checked"{/if}>
                    <input id="adv_type0" name="adv_type" value="2" type="radio" {if condition="$adv.adv_type eq 2"} checked="checked"{/if}>
                </div>
            </td>
            <td class="vatop tips"></td>
        </tr>
        <!-- 添加图片-->
        <tr class="noborder"  >
            <td class="required w120">图片:</td>
            <td class="vatop rowform">
                <div class="dssc-goods-default-pic">
                    <div class="goodspic-uplaod">
                        <span></span>
                        {if $adv.adv_images}
                        <img width="300px" height="200px" src="{$http_name}/{$adv.adv_images}" />
                        {/if}
                        <input type="hidden" name="images" id="images_name" value="{$adv.adv_images|default=''}"  />
                        <input type="file" hidefocus="true" name="images" id="images" data-action="{:url('Adv/ajax_images')}">
                        </span>
                        <p class="hint"></p>
                    </div>
                </div>
                <div id="dataLoading" class="wp_data_loading">
                    <div class="data_loading"></div>
                </div>
                <div id="demo">
                </div>
            </td>
            <td class="vatop tips">{$Think.lang.adv_edit_support}jpg,jpeg,png </td>
        </tr>

        {if isset($adv) && $adv.adv_address == 3}
        <tr id="fenlei1" class="noborder" >
            <td class="required"><label>所属分类:</label></td>
            <input type="hidden" id="adv_type" value="{$adv.ap_id}">
            <td class="vatop rowform">
                <select name="ap_id" id="ap_id1">
                    <option value='1' {if condition="$adv.ap_id eq 1"}selected{/if}>文章类</option>
                    <option value='5' {if condition="$adv.ap_id eq 5"}selected{/if}>视频类</option>
                </select>
            </td>
            <td class="vatop tips"></td>
        </tr>
        {else/}
        <tr id="fenlei" class="noborder" >
            <td class="required"><label>所属分类:</label></td>
            <input type="hidden" id="adv_type" value="{$adv.ap_id}">
            <td class="vatop rowform">
                <select name="ap_id" id="ap_id" >
                    <option value='1' {if condition="$adv.ap_id eq 1"}selected{/if}>文章类</option>
                    <option value='2' {if condition="$adv.ap_id eq 2"}selected{/if}>满减</option>
                    <option value='3' {if condition="$adv.ap_id eq 3"}selected{/if}>折扣</option>
                    <option value='4' {if condition="$adv.ap_id eq 4"}selected{/if}>项目定制</option>
                    <option value='5' {if condition="$adv.ap_id eq 5"}selected{/if}>视频类</option>
                </select>
            </td>
            <td class="vatop tips"></td>
        </tr>
        {/if}



        <!-- 获取所有文章-->
        <tr class="noborder" id="artice_type">
            <td class="required"><label>文章:</label></td>
            <td class="vatop rowform">
                <select name="artice_type">
                    {foreach name="article_info" item="ap" key="ap_k" }
                    <option value='{$ap.article_id}' {if condition="$adv.artice_type eq $ap.article_id"}selected{/if}>  {$ap.article_title}</option>
                    {/foreach}
                </select>
            </td>
            <td class="vatop tips"></td>
        </tr>
        <!-- 获取所有满减-->
        <tr class="noborder" id="mansong_type" style="display: none;">
            <td class="required"><label>满减:</label></td>
            <td class="vatop rowform">
                <select name="mansong_type" >
                    {foreach name="mansong" item="ap" key="ap_k" }
                    <option value='{$ap.mansong_id}' {if condition="$adv.mansong_type eq $ap.mansong_id"}selected{/if}>  {$ap.mansong_name}</option>
                    {/foreach}
                </select>
            </td>
            <td class="vatop tips"></td>
        </tr>
        <!-- 获取所有折扣-->
        <tr class="noborder" id="xianshi_type" style="display: none;">
            <td class="required"><label>折扣:</label></td>
            <td class="vatop rowform">
                <select name="xianshi_type" >
                    {foreach name="pxianshi" item="ap" key="ap_k" }
                    <option value='{$ap.xianshi_id}' {if condition="$adv.xianshi_type eq $ap.xianshi_id"}selected{/if}>  {$ap.xianshi_name}</option>
                    {/foreach}
                </select>
            </td>
            <td class="vatop tips"></td>
        </tr>
        <!-- 获取所有项目定制-->
        <tr class="noborder" id="project_type" style="display: none;">
            <td class="required"><label>项目定制:</label></td>
            <td class="vatop rowform">
                <select name="project_type" >
                    {foreach name="project_title" item="ap" key="ap_k" }
                    <option value='{$ap.project_id}' {if condition="$adv.project_type eq $ap.project_id"}selected{/if}>{$ap.project_title}</option>
                    {/foreach}
                </select>
            </td>
            <td class="vatop tips"></td>
        </tr>
        <!-- 添加视频-->
        <tr class="noborder"  id="video_type" style="display: none;">
            <td class="required w120">视频:</td>
            <td class="vatop rowform">
                <div class="dssc-goods-default-pic">
                    <div class="goodspic-uplaod">
                        <span></span>
                        {if $adv.video_type}
                        <video width="300" height="300" controls>
                                <source src="{$http_name}/{$adv.video_type}" type="video/mp4">
                        </video>
                        {/if}
                        <input type="hidden" name="videos" id="video_name"
                               />
                        <input type="file" hidefocus="true" name="videos" id="videos" data-action="{:url('Adv/ajax_videos')}">
                        </span>
                        <p class="hint"></p>
                    </div>
                </div>
                <div id="dataLoading" class="wp_data_loading">
                    <div class="data_loading"></div>
                </div>
                <div id="demo">
                </div>
            </td>
        </tr>


        <tr class="noborder">
            <td class="required"><label for="adv_startdate">{$Think.lang.ds_start_time}:</label></td>
            <td class="vatop rowform"><input type="text" name="adv_startdate" id="adv_startdate" value="{:date('Y-m-d',$adv.adv_startdate)}" class="txt"></td>
            <td class="vatop tips"></td>
        </tr>
        <tr class="noborder">
            <td class="required"><label for="adv_enddate">{$Think.lang.ds_end_time}:</label></td>
            <td class="vatop rowform"><input type="text" name="adv_enddate" id="adv_enddate" value="{:date('Y-m-d',$adv.adv_enddate)}" class="txt" ></td>
            <td class="vatop tips"></td>
        </tr>


        <tr class="noborder">
            <td class="required"><label for="adv_startdate">{$Think.lang.adv_sort}:</label></td>
            <td class="vatop rowform"><input type="text" name="adv_sort" id="adv_sort" class="txt" value="{$adv.adv_sort|default='0'}">
            </td>
            <td class="vatop tips">{$Think.lang.adv_sort_role}</td>
        </tr>
        <tr class="noborder">
            <td class="required"><label for="adv_startdate">{$Think.lang.adv_enabled}:</label></td>
            <td class="vatop rowform">
                <div class="onoff">
                    <label for="adv_enabled1" class="cb-enable {if condition="$adv.adv_enabled eq 1"}selected{/if}">{$Think.lang.ds_open}</label>
                    <label for="adv_enabled0" class="cb-disable {if condition="$adv.adv_enabled eq 0"}selected{/if}">{$Think.lang.ds_close}</label>
                    <input id="adv_enabled1" name="adv_enabled" value="1" type="radio" {if condition="$adv.adv_enabled eq 1"} checked="checked"{/if}>
                    <input id="adv_enabled0" name="adv_enabled" value="0" type="radio" {if condition="$adv.adv_enabled eq 0"} checked="checked"{/if}>
                </div>
            </td>
            <td class="vatop tips"></td>
        </tr>


      </tbody>
      <tfoot>
        <tr class="tfoot" style="text-align: center;">
            {if isset($adv) && $adv.adv_images !=''}
            <input  name="adv_id" value="{$adv.adv_id}" type="hidden">
            {/if}

            <td colspan="15"><input style="display:inline-block;border:none;padding:0px 18px;height:38px;line-height:38px;border-radius:3px;background-color:#419dfd;color:#fff!important;vertical-align:middle;text-align:center;cursor:pointer;font-size: 14px;" class="btn" id="submit"  type="submit" value="{$Think.lang.ds_submit}"/></td>
        </tr>
      </tfoot>
    </table>
  </form>
</div>
<link href="{$Think.PLUGINS_SITE_ROOT}/colorpicker/evol.colorpicker.css" rel="stylesheet" type="text/css">
<script src="{$Think.PLUGINS_SITE_ROOT}/colorpicker/evol.colorpicker.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var type = $("#adv_type").val();
        if(type == 1){
            $("#artice_type").show();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").hide();
        }else if(type == 2){
            $("#artice_type").hide();
            $("#mansong_type").show();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").hide();
        }else if(type == 3){
            $("#artice_type").hide();
            $("#mansong_type").hide();
            $("#xianshi_type").show();
            $("#project_type").hide();
            $("#video_type").hide();
        }else if(type == 4){
            $("#artice_type").hide();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").show();
            $("#video_type").hide();
        }else if(type == 5){
            $("#artice_type").hide();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").show();
        }

        $('#submit').on('click', function () {
         if($('#adv_form').valid()){
             var images = $('#images_name').val();
             if(images == ""){
                 layer.msg('请上传广告图片');return false;
             }
                 $('#adv_form').submit();
         }
     })
    });
$(function(){
    $('#adv_startdate').datepicker({dateFormat: 'yy-mm-dd'});
    $('#adv_enddate').datepicker({dateFormat: 'yy-mm-dd'});
    $('#adv_bgcolor').colorpicker({showOn: 'both'});
        $("#file_adv_code").change(function () {
            $("#textfield1").val($("#file_adv_code").val());
        });

    $('#adv_address').change(function(){
        var type = $(this).children('option:selected').val();//这就是selected的值
        if(type == 3){
            $("#ap_id").html(" <option value='1' >文章类</option>\n" +
                "               <option value='5'>视频类</option>");
           
        }else{
            $("#ap_id").html("<option value='1' >文章类</option>\n" +
                "            <option value='2' >满减</option>\n" +
                "            <option value='3' >折扣</option>\n" +
                "            <option value='4' >项目定制</option>\n" +
                "            <option value='5' >视频类</option>");
        }
    });

    $('#ap_id').change(function(){
        var type = $(this).children('option:selected').val();//这就是selected的值
        if(type == 1){
            $("#artice_type").show();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").hide();
        }else if(type == 2){
            $("#artice_type").hide();
            $("#mansong_type").show();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").hide();
        }else if(type == 3){
            $("#artice_type").hide();
            $("#mansong_type").hide();
            $("#xianshi_type").show();
            $("#project_type").hide();
            $("#video_type").hide();
        }else if(type == 4){
            $("#artice_type").hide();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").show();
            $("#video_type").hide();
        }else if(type == 5){
            $("#artice_type").hide();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").show();
        }
    });
    $('#ap_id1').change(function(){
        var type = $(this).children('option:selected').val();//这就是selected的值
        if(type == 1){
            $("#artice_type").show();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").hide();
        }else if(type == 5){
            $("#artice_type").hide();
            $("#mansong_type").hide();
            $("#xianshi_type").hide();
            $("#project_type").hide();
            $("#video_type").show();
        }
    });
});
</script>

<script>
    //按钮先执行验证再提交表单
    $(function(){
        $('#adv_form').validate({
            errorPlacement: function(error, element){
                error.appendTo(element.parent().parent().find('td:last'));
            },
            rules : {
                adv_name : {
                    required : true
                }
            },
            messages : {
                adv_name : {
                    required : '广告名称不能为空'
                }

            }
        });

        //上传图片
        $(document).on('change', '#images', function () {

            // $('#dataLoading').show();
            var files = document.getElementById('images').files[0];

            var formData = new FormData();
            var obj = $(this);
            formData.append('file', files);
            var url = $(this).attr('data-action');
            $.ajax({
                //请求方式
                type: "POST",
                //请求地址
                url: url,
                data: formData,
                cache: false,  //默认是true，但是一般不做缓存
                processData: false, //用于对data参数进行序列化处理，这里必须false；如果是true，就会将FormData转换为String类型
                contentType: false,  //一些文件上传http协议的关系，自行百度，如果上传的有文件，那么只能设置为false
                //请求成功
                success: function (result) {

                    if (result.msg == 1) {
                        $('.vide').empty();
                        $('#dataLoading').hide();
                        layer.msg('上传成功');
                        $("#images_name").val(result.files_name);
                        // $("type[name='old_filename']").val(result.old_name);
                    } else {
                        obj.val('');
                        $('#dataLoading').hide();
                        layer.msg('文件信息错误,请重新上传');
                        return false;
                    }

                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                    console.log(e.status);
                    console.log(e.responseText);
                }
            });

        });

        //上传
        $(document).on('change', '#videos', function () {

            // $('#dataLoading').show();
            var files = document.getElementById('videos').files[0];

            var formData = new FormData();
            var obj = $(this);
            formData.append('file', files);
            var url = $(this).attr('data-action');
            $.ajax({
                //请求方式
                type: "POST",
                //请求地址
                url: url,
                data: formData,
                cache: false,  //默认是true，但是一般不做缓存
                processData: false, //用于对data参数进行序列化处理，这里必须false；如果是true，就会将FormData转换为String类型
                contentType: false,  //一些文件上传http协议的关系，自行百度，如果上传的有文件，那么只能设置为false
                //请求成功
                success: function (result) {

                    if (result.msg == 1) {
                        $('.vide').empty();
                        $('#dataLoading').hide();
                        layer.msg('上传成功');
                        $("#video_name").val(result.files_name);
                        // $("type[name='old_filename']").val(result.old_name);
                    } else {
                        obj.val('');
                        $('#dataLoading').hide();
                        layer.msg('文件信息错误,请重新上传');
                        return false;
                    }

                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                    console.log(e.status);
                    console.log(e.responseText);
                }
            });

        });
    });


    $((function($){

        $.datepicker.regional['zh-CN'] = {

            clearText: '清除',

            clearStatus: '清除已选日期',

            closeText: '关闭',

            closeStatus: '不改变当前选择',

            prevText: '<上月',

            prevStatus: '显示上月',

            prevBigText: '<<',

            prevBigStatus: '显示上一年',

            nextText: '下月>',

            nextStatus: '显示下月',

            nextBigText: '>>',

            nextBigStatus: '显示下一年',

            currentText: '今天',

            currentStatus: '显示本月',

            monthNames: ['一月','二月','三月','四月','五月','六月', '七月','八月','九月','十月','十一月','十二月'],

            monthNamesShort: ['一','二','三','四','五','六', '七','八','九','十','十一','十二'],

            monthStatus: '选择月份',

            yearStatus: '选择年份',

            weekHeader: '周',

            weekStatus: '年内周次',

            dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],

            dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],

            dayNamesMin: ['日','一','二','三','四','五','六'],

            dayStatus: '设置 DD 为一周起始',

            dateStatus: '选择 m月 d日, DD',

            dateFormat: 'yy-mm-dd',

            firstDay: 1,

            initStatus: '请选择日期',

            showMonthAfterYear:true,//是否把月放在年的后面

            yearSuffix: '年', //年的后缀

            isRTL: false

        };

        $.datepicker.setDefaults($.datepicker.regional['zh-CN']);

    })(jQuery));
</script>